<template>
  <view>
    <!-- 搜索组件 -->
    <view class="search-com">
      <shop-search @click="toSearchPage"></shop-search>
    </view>

    <!-- 轮播图区域 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(item, i) in swiperList" :key="i">
        <navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id ">
          <img :src="item.image_src" alt="">
        </navigator>
      </swiper-item>
    </swiper>

    <!-- 分类导航区域 -->
    <view class="nav-list">
      <div class="nav-item" v-for="(item, i) in navList" :key="i" @click="navClickHandle(item)">
        <img class="nav-img" :src="item.image_src" alt="">
      </div>
    </view>

    <!-- 楼层区域 -->
    <div class="floor-list">
      <div class="floor-item" v-for="(item, i) in floorList" :key="i">
        <!-- 标题 -->
        <img :src="item.floor_title.image_src" alt="" class="floor-title">
        <!-- 图片 -->
        <div class="floor-img">
          <!-- 左侧 -->
          <navigator :url="item.product_list[0].url" class="left-img">
            <img :src="item.product_list[0].image_src" alt=""
              :style="{ width: item.product_list[0].image_width + 'rpx' }" mode="widthFix">
          </navigator>
          <!-- 右侧 -->
          <div class="right-img">
            <navigator :url="item2.url" class="right-img-item" v-for="(item2, i2) in item.product_list" :key="i2"
              v-if="i2 !== 0">
              <img :src="item2.image_src" alt="" :style="{ width: item.product_list[0].image_width + 'rpx' }"
                mode="widthFix">
            </navigator>
          </div>
        </div>
      </div>
    </div>
  </view>
</template>

<script>
  import
  badgeMix
  from "@/mixins/tabbar-badge.js"
  export default {
    mixins: [badgeMix],
    data() {
      return {
        swiperList: [],
        navList: [],
        floorList: []
      };
    },
    onLoad() {
      this.getSwiperList(),
        this.getNavList(),
        this.getFloorList()
    },
    methods: {

      // 获取轮播图数据
      async getSwiperList() {
        const {
          data: res
        } = await uni.$http.get("/api/public/v1/home/swiperdata")
        // 请求失败
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        // 请求成功
        this.swiperList = res.message
        uni.$showMsg("数据请求成功！")
      },

      // 获取导航栏数据
      async getNavList() {
        const {
          data: res
        } = await uni.$http.get("/api/public/v1/home/catitems")
        // 请求失败
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        // 请求成功
        this.navList = res.message
        uni.$showMsg("数据请求成功！")
      },

      // 跳转到分类页面
      navClickHandle(item) {
        if (item.name === "分类") {
          uni.switchTab({
            url: "/pages/cate/cate"
          })
        }
      },

      // 获取楼层数据
      async getFloorList() {
        const {
          data: res
        } = await uni.$http.get("/api/public/v1/home/floordata")
        // 请求失败
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        // 请求成功
        this.floorList = res.message
        this.floorList.forEach(item => {
          item.product_list.forEach(item => {
            item.url = "/subpkg/goods_list/goods_list?" + item.navigator_url.split('?')[1]
          })
        })
        uni.$showMsg("数据请求成功！")
      },

      // 跳转到搜索页面
      toSearchPage() {
        uni.navigateTo({
          url: '/subpkg/search/search'
        });
      }
    }
  }
</script>

<style lang="scss">
  .search-com {
    position: sticky;
    top: 0;

    z-index: 99;
  }

  swiper {
    height: 330rpx;

    .swiper-item,
    img {
      width: 100%;
      height: 100%;
    }
  }

  .nav-list {
    display: flex;
    justify-content: space-around;
    margin: 15rpx 0;

    .nav-img {
      width: 128rpx;
      height: 140rpx;
    }
  }

  .floor-title {
    width: 100%;
    height: 60rpx;
  }

  .floor-img {
    display: flex;
    padding-left: 10rpx;

    .right-img {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-around;
    }
  }
</style>